package com.tanovo.service.impl;

import com.tanovo.bo.SwithcDataInfoBo;
import com.tanovo.bo.TokenRequest;
import com.tanovo.bo.UserCore;
import com.tanovo.config.UserInfoContext;
import com.tanovo.domain.*;
import com.tanovo.exception.ServiceException;
import com.tanovo.mapper.OauthClientTokenMapper;
import com.tanovo.service.ICompanyService;
import com.tanovo.service.IOauthClientDetailsService;
import com.tanovo.service.IOauthClientTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class OauthClientTokenServiceImpl implements IOauthClientTokenService {
    @Autowired
    private OauthClientTokenMapper oauthClientTokenMapper;
    @Autowired
    private ICompanyService iCompanyService;
    @Autowired
    private IOauthClientDetailsService oauthClientDetailsService;
    @Override
    public OauthClientToken getOauthClientToken(TokenRequest tokenRequest) {
        OauthClientTokenExample example = new OauthClientTokenExample();
        example.createCriteria()
                .andUserIdEqualTo(tokenRequest.getUserId().toString());
        List<OauthClientToken> list = oauthClientTokenMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void saveToken(OauthClientToken oauthClientToken) {
        oauthClientTokenMapper.insertSelective(oauthClientToken);
    }

    @Override
    public OauthClientToken getByToken(String token) {
        OauthClientTokenExample example = new OauthClientTokenExample();
        example.createCriteria().andTokenEqualTo(token);
        List<OauthClientToken> tokenList = oauthClientTokenMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(tokenList)) {
            return null;
        }
        return tokenList.get(0);
    }

    @Override
    public void deleteToken(OauthClientToken clientToken) {
        oauthClientTokenMapper.deleteByPrimaryKey(clientToken.getId());
    }

    @Override
    @Transactional
    public void swithcDataInfo(SwithcDataInfoBo swithcDataInfoBo) {
        UserCore user = UserInfoContext.getUser();
        if(swithcDataInfoBo.getCompanyId()!=null){
            List<CompanyMapUser> companyList= iCompanyService.getCompanyMapUserList(user.getId());
            if(CollectionUtils.isEmpty(companyList)){
                throw  new ServiceException("公司id不正确");
            }
            Boolean flag=false;
            for (CompanyMapUser c:companyList) {
                 if(c.getCompanyId()==swithcDataInfoBo.getCompanyId()){
                     flag=true;
                 }
            }
            if(!flag){
                throw  new ServiceException("公司id不正确");
            }
            for (CompanyMapUser c:companyList) {
                if(c.getCompanyId()==swithcDataInfoBo.getCompanyId()){
                    c.setLoginDefault(1);
                }else {
                    c.setLoginDefault(0);
                }
                c.setUpdateBy(user.getId().toString());
            }
            iCompanyService.updateCompanyMapUserList(companyList);
        }
        if(!StringUtils.isEmpty(swithcDataInfoBo.getClientId())){
            List<ClientMapUser> userClientList= oauthClientDetailsService.getUserClientList(user.getId(),user.getCompanyId(),swithcDataInfoBo.getClientId());
            if(CollectionUtils.isEmpty(userClientList)){
                throw  new ServiceException("请开通对应客户端权限");
            }
        }
        OauthClientTokenExample example = new OauthClientTokenExample();
        example.createCriteria()
                .andUserIdEqualTo(user.getId().toString())
                .andDelFlagEqualTo(0);
        List<OauthClientToken> list = oauthClientTokenMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            OauthClientToken oauthClientToken = list.get(0);
            oauthClientToken.setClientId(swithcDataInfoBo.getClientId());
            oauthClientToken.setCompanyId(swithcDataInfoBo.getCompanyId());
            oauthClientTokenMapper.updateByPrimaryKeySelective(oauthClientToken);
        }

    }

    @Override
    public void updateToken(OauthClientToken clientToken) {
        oauthClientTokenMapper.updateByPrimaryKeySelective(clientToken);
    }
}
